﻿using AiShow.BLL;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AiShow.Commen;
using AiShow.Model.OperResult;

namespace AiShow.WebSite
{
    public partial class Login : System.Web.UI.Page
    {
        IAccountManager AccountManager = new AccountManager();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid) {
               var result= AccountManager.LoginByUserName(txtUserName.Text, txtPassword.Text);
                if (result.StatusCode == StatusCode.Succeed)
                {
                    var user= result.ReadResult();
                    IdentityManager.SaveUser(user);
                    var url = Request.QueryString["ReturnUrl"];
                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        Response.Redirect(url);
                    }
                    else
                    {
                        Response.Redirect("Default.aspx");
                    }
                }
                else {
                    ClientScript.RegisterStartupScript(GetType(), "loginerror", $"alert('{result.Message }')",true);
                }
            }

        }
    }
}